var express = require('express');
var router = express.Router();
var https = require('https');
// var querystring = require('querystring');

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://127.0.0.1:27017/project';

router.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', 'Content-Type');
  next();
});
/* GET home page. */
router.get('/', function (req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/entries', function (req, res, next) {

  var data = '';
  console.log(req.url);
  // console.log(querystring.stringify(req.query));
  // var str = querystring.stringify(req.query);
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?' + queryStr, function (response) {

    response.on('data', function (chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })

  })

})
router.get('/msitelist', function (req, res, next) {

  var data = '';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/restaurants?' + queryStr,function(response){

    response.on('data', function (chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })

  })

})
//discover发现页面
router.get('/discover',function(req,res,next){
  var data = '';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/likes?' + queryStr + '&offset=0&limit=3&request_id=93b0527a-93ef-47c7-a2d9-8f7415ab033e&tag_id=-1&columns=1',function(response){

    response.on('data', function (chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })

  })
})

router.get('/filterlist', function (req, res, next) {

  var data = '';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/v2/restaurant/category?' + queryStr, function(response){
    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })
  })
})

router.get('/shoplist', function (req, res, next) {

  var data = '';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  if(req.query.support_ids){
    req.query.support_ids.forEach(function(item) {
      queryStr += '&support_ids[]=' + item
    });
  }
  https.get('https://mainsite-restapi.ele.me/shopping/restaurants?' + queryStr,function(response){
    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })
  })
})
//shop
router.get('/shop', function (req, res, next) {

  var data = '';
  var latitude = req.query.latitude;
  var longitude = req.query.longitude;
  var id = req.query.restaurant_id
  https.get('https://mainsite-restapi.ele.me/shopping/restaurant/'+ id +'?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude='+latitude+'&longitude='+longitude,function(response){

    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end',function(){
      res.send(data);
    })

  })
})

//shopmenu
router.get('/shopmenu',function(req,res,next){

  var data = '';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/v2/menu?' + queryStr,function(response){
    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end',function(){
      res.send(data);
    })

  })
})

router.get('/district',function(req,res,next){

  var data = '';
  var keyword = req.query.keyword;
  var longitude = req.query.longitude;
  var latitude = req.query.latitude;
  console.log(req.query);
  https.get('https://mainsite-restapi.ele.me/bgs/poi/search_poi_nearby?keyword='+ keyword +'&offset=0&limit=20&longitude='+ longitude +'&latitude='+ latitude ,function(response){
    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })

  })
})

//搜索列表请求数据
router.get('/searchList',function(req,res,next){
   var data = '';
   var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/v1/restaurants/search?offset=0&limit=20&'+ queryStr +'&search_item_type=2&extra[]=activities', function (response) {
    response.on('data', function (chunk) {
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })
  })
})

// 搜索页面请求数据
router.get('/search',function(req,res,next){
  var data='';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/shopping/v3/hot_search_words?geohash=ws105rz9smwm&' + queryStr,function(response) {
    response.on('data',function(chunk){
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })
  })
})

//天气请求数据
router.get('/weather',function(req,res,next){
  var data='';
  var queryArr = req.url.split('?');
  var queryStr = queryArr[1];
  https.get('https://mainsite-restapi.ele.me/bgs/weather/current?' + queryStr,function(response) {
    response.on('data',function(chunk){
      data += chunk;
    });
    response.on('end', function () {
      res.send(data);
    })
  })
})

// 注册登录存入数据库
router.get('/note', function (req, res, next) {
  var tel = req.query.tel;
  MongoClient.connect(DB_CONN_STR, function (err, db) {
    if (err) {
      res.send({ msg: '网络异常，请稍后重试' });
    } else {
      var conn = db.collection('user');
      conn.find({ tel: tel }).count(function (err, num) {
        if (err) {
          res.send({ msg: '网络异常，请稍后重试' });
        } else {
          if (num > 0) {
            res.send({ msg: 'find' });
          } else {
            conn.insertOne({ tel: tel }, function (error, info) {
              if (error) {
                res.send({ msg: 'no' })
              } else {
                res.send({ msg: 'yes' })
              }
              db.close();
            })
          }
        }
      })
    }
  })
})
//美食热推头部页面
router.get('/ulike',function(req,res,next){
  var data = '';
  https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/banner?latitude=22.533012&longitude=113.930475&request_id=042ee169-6f25-4c2f-8b73-3f3df9ddafa2',function(response){

    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end',function(){
      res.send(data);
    })

  })
})
//美食热推头部主体
router.get('/ulikeview',function(req,res,next){
  var data = '';
  https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/likes?latitude=22.533012&longitude=113.930475&offset=0&limit=10&columns=2&request_id=042ee169-6f25-4c2f-8b73-3f3df9ddafa2&tag_id=-1&extras=%5B%22activities%22%5D',function(response){

    response.on('data',function(chunk) {
      data += chunk;
    });
    response.on('end',function(){
      res.send(data);
    })

  })
})

module.exports = router;
